import requests
import re
import json

def get_like_count(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8'
    }
    
    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        
        # 使用正则表达式匹配 meta 标签中的点赞数
        pattern = r'<meta name="og:xhs:note_like" content="(\d+)">'
        match = re.search(pattern, response.text)
        
        if match:
            return {
                'success': True,
                'like_count': match.group(1),
                'url': url
            }
        
        # 如果没找到，保存响应内容以便调试
        with open('response.html', 'w', encoding='utf-8') as f:
            f.write(response.text)
            
        return {
            'success': False,
            'error': '未找到点赞数，响应已保存到response.html',
            'url': url
        }
            
    except Exception as e:
        return {
            'success': False,
            'error': f'请求失败: {str(e)}',
            'url': url
        }

def main():
    url = input("请输入小红书文章链接: ")
    result = get_like_count(url)
    print(json.dumps(result, ensure_ascii=False, indent=2))

if __name__ == "__main__":
    main() 